package com.iot.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.iot.commons.ResponseResult;
import com.iot.enums.StatusEnum;
import com.iot.mapper.UserInfoMapper;
import com.iot.model.dto.UserInfoDto;
import com.iot.model.entity.UserInfoEntity;
import com.iot.model.query.UserInfoQuery;
import com.iot.model.vo.AuthInfoVo;
import com.iot.model.vo.UserInfoVo;
import com.iot.service.UserInfoService;
import com.iot.utils.DateUtil;
import com.iot.utils.ResponseResultUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * UserInfoServiceImpl
 *
 * @author: dh
 * @date: 2024-04-20 17:20:48
 */
@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl implements UserInfoService {

    private final UserInfoMapper userInfoMapper;

    @Override
    public ResponseResult getList(UserInfoQuery userInfo) {
        Page<UserInfoEntity> page = new Page<>(userInfo.getPageIndex(), userInfo.getPageSize());
        LambdaQueryWrapper<UserInfoEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserInfoEntity::getState, StatusEnum.VALID.getKey());
        queryWrapper.orderByDesc(UserInfoEntity::getCreateTime);
        IPage<UserInfoEntity> userInfoPage = userInfoMapper.selectPage(page, queryWrapper);
        List<UserInfoVo> userInfoVoList = Lists.newArrayList();
        for (UserInfoEntity userInfoEntity : userInfoPage.getRecords()) {
            UserInfoVo vo = new UserInfoVo();
            vo.setUserName(userInfoEntity.getUserName());
            vo.setNickName(userInfoEntity.getNickName());
            vo.setAvatarsUrl(userInfoEntity.getAvatarsUrl());
            vo.setSex(userInfoEntity.getSex());
            vo.setAge(userInfoEntity.getAge());
            vo.setBirthYear(userInfoEntity.getBirthYear());
            vo.setHeight(userInfoEntity.getHeight());
            vo.setWeight(userInfoEntity.getWeight());
            vo.setStep(userInfoEntity.getStep());
            vo.setGoalStep(userInfoEntity.getGoalStep());
            vo.setUnit(userInfoEntity.getUnit());
            userInfoVoList.add(vo);
        }

        return ResponseResultUtil.success(userInfoPage.getTotal(), userInfoVoList);
    }

    @Override
    public ResponseResult edit(UserInfoDto userInfo) {
        UserInfoEntity entity = new UserInfoEntity();
        AuthInfoVo authInfoEntity = (AuthInfoVo) StpUtil.getSession().get("loginInfo");
        if (null == authInfoEntity) {
            return ResponseResultUtil.authInfoFailure();
        }
        LambdaUpdateWrapper<UserInfoEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(UserInfoEntity::getAuthId, authInfoEntity.getId());
        entity.setUserName(userInfo.getUserName());
        entity.setNickName(userInfo.getNickName());
        entity.setAvatarsUrl(userInfo.getAvatarsUrl());
        entity.setSex(userInfo.getSex());
        entity.setAge(userInfo.getAge());
        entity.setBirthYear(userInfo.getBirthYear());
        entity.setHeight(userInfo.getHeight());
        entity.setWeight(userInfo.getWeight());
        entity.setStep(userInfo.getStep());
        entity.setGoalStep(userInfo.getGoalStep());
        entity.setUnit(userInfo.getUnit());
        entity.setUpdateTime(DateUtil.now());
        userInfoMapper.update(entity, wrapper);
        return ResponseResultUtil.success();
    }

    @Override
    public ResponseResult cid(UserInfoDto userInfo) {
        AuthInfoVo authInfoEntity = (AuthInfoVo) StpUtil.getSession().get("loginInfo");
        if (null == authInfoEntity) {
            return ResponseResultUtil.authInfoFailure();
        }
        UserInfoEntity entity = new UserInfoEntity();
        LambdaUpdateWrapper<UserInfoEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(UserInfoEntity::getAuthId, authInfoEntity.getId());

        entity.setCid(userInfo.getCid());
        entity.setUpdateTime(DateUtil.now());
        userInfoMapper.update(entity, wrapper);
        return ResponseResultUtil.success();
    }

    @Override
    public ResponseResult cidClear() {
        AuthInfoVo authInfoEntity = (AuthInfoVo) StpUtil.getSession().get("loginInfo");
        if (null == authInfoEntity) {
            return ResponseResultUtil.authInfoFailure();
        }
        UserInfoEntity entity = new UserInfoEntity();
        LambdaUpdateWrapper<UserInfoEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(UserInfoEntity::getAuthId, authInfoEntity.getId());
        wrapper.set(UserInfoEntity::getCid,null);

        entity.setUpdateTime(DateUtil.now());
        userInfoMapper.update(entity, wrapper);
        return ResponseResultUtil.success();
    }


    @Override
    public ResponseResult editByAuthId(UserInfoDto userInfo) {
        LambdaUpdateWrapper<UserInfoEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(UserInfoEntity::getAuthId, userInfo.getAuthId());

        UserInfoEntity entity = new UserInfoEntity();
        entity.setUserName(userInfo.getUserName());
        entity.setNickName(userInfo.getNickName());
        entity.setAvatarsUrl(userInfo.getAvatarsUrl());
        entity.setSex(userInfo.getSex());
        entity.setAge(userInfo.getAge());
        entity.setBirthYear(userInfo.getBirthYear());
        entity.setHeight(userInfo.getHeight());
        entity.setWeight(userInfo.getWeight());
        entity.setStep(userInfo.getStep());
        entity.setGoalStep(userInfo.getGoalStep());
        entity.setUnit(userInfo.getUnit());
        entity.setIsOn(userInfo.getIsOn());
        entity.setUpdateTime(DateUtil.now());
        userInfoMapper.update(entity, wrapper);
        return ResponseResultUtil.success();
    }

    @Override
    public ResponseResult add(UserInfoDto userInfo) {
        UserInfoEntity entity = new UserInfoEntity();
        entity.setAuthId(userInfo.getAuthId());
        entity.setUserName(userInfo.getUserName());
        entity.setNickName(userInfo.getNickName());
        entity.setAvatarsUrl(userInfo.getAvatarsUrl());
        entity.setSex(userInfo.getSex());
        entity.setAge(userInfo.getAge());
        entity.setBirthYear(userInfo.getBirthYear());
        entity.setHeight(userInfo.getHeight());
        entity.setWeight(userInfo.getWeight());
        entity.setStep(userInfo.getStep());
        entity.setGoalStep(userInfo.getGoalStep());
        entity.setUnit(userInfo.getUnit());
        entity.setIsOn(0);
        entity.setState(StatusEnum.VALID.getKey());
        entity.setCreateTime(DateUtil.now());
        userInfoMapper.insert(entity);
        return ResponseResultUtil.success();
    }

    @Override
    public ResponseResult getById(UserInfoQuery userInfo) {
        AuthInfoVo authInfoEntity = (AuthInfoVo) StpUtil.getSession().get("loginInfo");
        if (null == authInfoEntity) {
            return ResponseResultUtil.authInfoFailure();
        }
        UserInfoVo vo = new UserInfoVo();
        LambdaQueryWrapper<UserInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserInfoEntity::getAuthId, authInfoEntity.getId());
        wrapper.eq(UserInfoEntity::getState, StatusEnum.VALID.getKey());
        wrapper.orderByDesc(UserInfoEntity::getCreateTime);
        wrapper.last("limit 1");
        UserInfoEntity userInfoEntity = userInfoMapper.selectOne(wrapper);
        if (null == userInfoEntity) {
            return ResponseResultUtil.success(vo);
        }
        vo.setUserName(userInfoEntity.getUserName());
        vo.setNickName(userInfoEntity.getNickName());
        vo.setAvatarsUrl(userInfoEntity.getAvatarsUrl());
        vo.setSex(userInfoEntity.getSex());
        vo.setAge(userInfoEntity.getAge());
        vo.setBirthYear(userInfoEntity.getBirthYear());
        vo.setHeight(userInfoEntity.getHeight());
        vo.setWeight(userInfoEntity.getWeight());
        vo.setStep(userInfoEntity.getStep());
        vo.setGoalStep(userInfoEntity.getGoalStep());
        vo.setUnit(userInfoEntity.getUnit());
        return ResponseResultUtil.success(vo);
    }

    @Override
    public UserInfoEntity getByUid(Long uid) {
        LambdaQueryWrapper<UserInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserInfoEntity::getId, uid);
        wrapper.eq(UserInfoEntity::getState, StatusEnum.VALID.getKey());
        wrapper.orderByDesc(UserInfoEntity::getCreateTime);
        wrapper.last("limit 1");
        return userInfoMapper.selectOne(wrapper);
    }

    @Override
    public UserInfoVo getByAuthId(Long authId) {
        UserInfoVo vo = new UserInfoVo();
        LambdaQueryWrapper<UserInfoEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserInfoEntity::getAuthId, authId);
        queryWrapper.eq(UserInfoEntity::getState, StatusEnum.VALID.getKey());
        queryWrapper.orderByDesc(UserInfoEntity::getCreateTime);
        queryWrapper.last("limit 1");
        UserInfoEntity userInfoEntity = userInfoMapper.selectOne(queryWrapper);
        if (null == userInfoEntity) {
            return vo;
        }
        vo.setUid(userInfoEntity.getId());
        vo.setUserName(userInfoEntity.getUserName());
        vo.setNickName(userInfoEntity.getNickName());
        vo.setAvatarsUrl(userInfoEntity.getAvatarsUrl());
        vo.setSex(userInfoEntity.getSex());
        vo.setAge(userInfoEntity.getAge());
        vo.setBirthYear(userInfoEntity.getBirthYear());
        vo.setHeight(userInfoEntity.getHeight());
        vo.setWeight(userInfoEntity.getWeight());
        vo.setPhone(userInfoEntity.getPhone());
        vo.setStep(userInfoEntity.getStep());
        vo.setGoalStep(userInfoEntity.getGoalStep());
        vo.setUnit(userInfoEntity.getUnit());
        vo.setCid(userInfoEntity.getCid());
        return vo;
    }


    @Override
    public Integer getShareStatus(Long authId) {
        LambdaQueryWrapper<UserInfoEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserInfoEntity::getAuthId, authId);
        queryWrapper.eq(UserInfoEntity::getState, StatusEnum.VALID.getKey());
        queryWrapper.orderByDesc(UserInfoEntity::getCreateTime);
        queryWrapper.last("limit 1");
        UserInfoEntity userInfoEntity = userInfoMapper.selectOne(queryWrapper);
        if (null == userInfoEntity) {
            return null;
        }
        return userInfoEntity.getIsOn();
    }

    @Override
    public UserInfoEntity getByPhone(String phone) {
        LambdaQueryWrapper<UserInfoEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(UserInfoEntity::getPhone, phone);
        queryWrapper.eq(UserInfoEntity::getState, StatusEnum.VALID.getKey());
        queryWrapper.orderByDesc(UserInfoEntity::getCreateTime);
        queryWrapper.last("limit 1");
        return userInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserInfoEntity> getByAuthIds(List<Long> authIds) {
        LambdaQueryWrapper<UserInfoEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(UserInfoEntity::getAuthId, authIds);
        queryWrapper.eq(UserInfoEntity::getState, StatusEnum.VALID.getKey());
        queryWrapper.orderByDesc(UserInfoEntity::getCreateTime);
        return userInfoMapper.selectList(queryWrapper);
    }

}
